<?php
/**
 * 陈杰  18323491246
 */

namespace App\Http\Controllers;

use App\Models\Interview\InterviewAnswerModel;
use App\Models\Interview\InterviewCateModel;
use App\Models\Interview\InterviewCommentModel;
use App\Models\Interview\InterviewCommentSubModel;
use App\Models\Interview\InterviewExpModel;
use App\Models\Interview\InterviewMemberModel;
use App\Models\Interview\InterviewModel;
use App\Models\Interview\InterviewRoleModel;
use App\Models\Interview\InterviewTopicModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class InterviewController extends Controller
{

    public function roleEdit(Request $request)
    {
        $rules        = [
            'id'    => 'required|integer|min:1',
            'name'  => 'required|max:25',
            'money' => 'required|numeric|min:0',
            'count' => 'required|integer|min:1',
        ];
        $sqlmap       = self::checkValidate($rules, $request->all());
        $model        = InterviewRoleModel::find($sqlmap['id']);
        $model->name  = $sqlmap['name'];
        $model->money = $sqlmap['money'];
        $model->count = $sqlmap['count'];

        try {
            $model->save();
            return self::successMsg();
        } catch (\Exception $exception) {
            return self::errorMsg($exception->getMessage());
        }
    }

    public function roleList(Request $request)
    {
        $data = InterviewRoleModel::get();
        return self::returnMsg($data);
    }

    public function expDel(Request $request)
    {
        $id = (int)$request->input('id');
        if ($id < 1)
            return self::errorMsg('请输入正确的回复id');
        if (InterviewExpModel::where('id', $id)->delete())
            return self::successMsg();
        return self::errorMsg();
    }

    public function expList(Request $request)
    {
        $where = function ($query) use ($request) {
            //筛选查询关键字
            if ($request->has('keywords') and $request->keywords != '') {
                $keywords = "%" . $request->keywords . "%";
                $query->orWhere('interview_comment_sub.id', '=', $request->keywords)
                    ->orWhere('interview_comment_sub.member_id', '=', $request->keywords)
                    ->orWhere('member.phone', '=', $request->keywords)
                    ->orWhere('member.name', '=', $request->keywords)
                    ->orWhere('interview_comment_sub.comment_id', '=', $request->keywords);
            }
        };
        $order = 'id';
        if ($request->has('order') and $request->order != '') {
            $order = 'interview_exp.' . $request->order;
        }
        $data = InterviewExpModel::where($where)
            ->join('member', 'interview_exp.member_id', '=', 'member.id')
            ->select('member.name as member_name', 'member.phone as member_phone', 'interview_exp.*')
            ->orderByDesc($order)
            ->paginate($request->get('count'));
        return self::returnMsg($data);
    }

    public function commentSubDel(Request $request)
    {
        $id = (int)$request->input('id');
        if ($id < 1)
            return self::errorMsg('请输入正确的回复id');
        if (InterviewCommentSubModel::where('id', $id)->delete())
            return self::successMsg();
        return self::errorMsg();
    }

    public function commentSubList(Request $request)
    {
        $firstWhere = function ($query) use ($request) {
            if ((int)$request->input('comment_id') > 0) {
                $query->where('interview_comment_sub.comment_id', (int)$request->input('comment_id'));
            }
        };
        $where      = function ($query) use ($request) {
            //筛选查询关键字
            if ($request->has('keywords') and $request->keywords != '') {
                $keywords = "%" . $request->keywords . "%";
                $query->orWhere('interview_comment_sub.id', '=', $request->keywords)
                    ->orWhere('interview_comment_sub.member_id', '=', $request->keywords)
                    ->orWhere('member.phone', '=', $request->keywords)
                    ->orWhere('member.name', '=', $request->keywords)
                    ->orWhere('interview_comment_sub.comment_id', '=', $request->keywords);
            }
        };
        $order      = 'id';
        if ($request->has('order') and $request->order != '') {
            $order = 'interview_comment_sub.' . $request->order;
        }

        $data = InterviewCommentSubModel::where($firstWhere)
            ->where($where)
            ->join('member', 'interview_comment_sub.member_id', '=', 'member.id')
            ->select('member.name as member_name', 'member.phone as member_phone', 'interview_comment_sub.*')
            ->orderByDesc($order)
            ->paginate($request->get('count'));
        return self::returnMsg($data);
    }

    public function commentList(Request $request)
    {
        $firstWhere = function ($query) use ($request) {
            if ((int)$request->input('pid') > 0 and (int)$request->input('type') > 0) {
                $query->where('interview_comment.pid', (int)$request->input('pid'))
                    ->where('interview_comment.type', (int)$request->input('type'));
            }
        };
        $where      = function ($query) use ($request) {
            //筛选查询关键字
            if ($request->has('keywords') and $request->keywords != '') {
                $keywords = "%" . $request->keywords . "%";
                $query->orWhere('interview_comment.id', '=', $request->keywords)
                    ->orWhere('interview_comment.member_id', '=', $request->keywords)
                    ->orWhere('member.phone', '=', $request->keywords)
                    ->orWhere('member.name', '=', $request->keywords)
                    ->orWhere('interview_comment.pid', '=', $request->keywords);
            }
        };
        $order      = 'id';
        if ($request->has('order') and $request->order != '') {
            $order = 'interview_comment.' . $request->order;
        }

        $data = InterviewCommentModel::where($firstWhere)
            ->where($where)
            ->join('member', 'interview_comment.member_id', '=', 'member.id')
            ->select('member.name as member_name', 'member.phone as member_phone', 'interview_comment.*')
            ->orderByDesc($order)
            ->paginate($request->get('count'));
        return self::returnMsg($data);
    }

    public function commentDel(Request $request)
    {
        $id = (int)$request->input('id');
        if ($id < 1)
            return self::errorMsg('请输入正确的评论id');
        if (InterviewCommentModel::where('id', $id)->delete())
            return self::successMsg();
        return self::errorMsg();
    }

    public function answerDel(Request $request)
    {
        $id = (int)$request->input('id');
        if ($id < 1)
            return self::errorMsg('请输入正确的id');
        if (InterviewAnswerModel::where('id', $id)->delete())
            return self::returnMsg();
        return self::errorMsg();
    }

    public function answerList(Request $request)
    {
        $firstWhere = function ($query) use ($request) {
            if ($request->has('topic_id') and (int)$request->topic_id > 0) {
                $query->where('interview_answer.topic_id', $request->topic_id);
            }
            if ($request->has('type') and (int)$request->type > 0) {
                $query->where('interview_answer.type', $request->type);
            }
            if ($request->has('voice') and $request->voice != '') {
                $query->where('interview_answer.voice', $request->voice);
            }
        };
        $where      = function ($query) use ($request) {
            //筛选查询关键字
            if ($request->has('keywords') and $request->keywords != '') {
                $keywords = "%" . $request->keywords . "%";
                $query->where('interview_answer.id', '=', $request->keywords)
                    ->orWhere('interview_answer.member_id', '=', $request->keywords)
                    ->orWhere('member.phone', '=', $request->keywords)
                    ->orWhere('member.name', '=', $request->keywords)
                    ->orWhere('interview_answer.topic_id', '=', $request->keywords);
            }
        };
        $order      = 'id';
        if ($request->has('order') and $request->order != '') {
            $order = 'interview_answer.' . $request->order;
        }
        $data = InterviewAnswerModel::where($firstWhere)
            ->where($where)
            ->join('member', 'interview_answer.member_id', '=', 'member.id')
            ->join('interview_topic', 'interview_answer.topic_id', '=', 'interview_topic.id')
            ->orderByDesc($order)
            ->select('interview_answer.*', 'member.phone as member_phone', 'member.name as member_name', 'interview_topic.content as topic_content')
            ->paginate($request->get('count'));
        return self::returnMsg($data);
    }

    public function topicEdit(Request $request)
    {
        $rules = [
            'content' => 'required|max:255',
            'cate_id' => 'required|integer|min:1',
            'type'    => 'required|integer|min:1',
        ];
        $v     = Validator::make($request->all(), $rules);
        if ($v->fails())
            return self::errorMsg($v->errors()->first());
        $sqlmap = $v->validated();

        $id = (int)$request->input('id');
        if ($id > 0) {
            //编辑
            $model = InterviewTopicModel::find($id);
            if (!isset($model))
                return self::errorMsg('没有找到该题目');
            $model->update_time = time();

        } else {
            $model              = new InterviewTopicModel();
            $model->create_time = time();
        }
        $model->content = $sqlmap['content'];
        $model->cate_id = $sqlmap['cate_id'];
        $model->type    = $sqlmap['type'];
        try {
            $model->save();
            return self::successMsg();
        } catch (\Exception $exception) {
            return self::errorMsg($exception->getMessage());
        }
    }

    public function topicDel(Request $request)
    {
        $id = (int)$request->input('id');
        if ($id < 1)
            return self::errorMsg('请输入正确的id');
        if (InterviewTopicModel::where('id', $id)->delete())
            return self::returnMsg();
        return self::errorMsg();
    }

    public function topicList(Request $request)
    {
        $where = function ($query) use ($request) {
            //筛选查询关键字
            if ($request->has('keywords') and $request->keywords != '') {
                $query->where('interview_topic.id', '=', $request->keywords);
            }
            if ($request->has('cate_id') and $request->cate_id != '') {
                $query->where('interview_topic.cate_id', '=', $request->cate_id);
            }
            if ($request->has('type') and $request->type != '') {
                $query->where('interview_topic.type', '=', $request->type);
            }
        };

        $data = InterviewTopicModel::where($where)->paginate($request->get('count'));
        return self::returnMsg($data);
    }

    public function interviewList(Request $request)
    {
        $where = function ($query) use ($request) {
            //筛选查询关键字
            if ($request->has('keywords') and $request->keywords != '') {
                $query->orWhere('interview.id', '=', $request->keywords)
                    ->orWhere('interview.member_id', '=', $request->keywords)
                    ->orWhere('member.phone', '=', $request->keywords)
                    ->orWhere('member.name', '=', $request->keywords);
            }

            if ($request->has('status') and $request->status !== '') {
                $query->where('interview.status', (int)$request->status);
            }

            if ($request->has('role') and $request->role !== '') {
                $query->where('interview.role', (int)$request->role);
            }
        };
        $order = 'id';
        if ($request->has('order') and $request->order != '') {
            $order = 'interview.' . $request->order;
        }
        $data = InterviewModel::where($where)
            ->join('member', 'interview.member_id', '=', 'member.id')
            ->leftJoin('interview_member', 'interview.id', '=', 'interview_member.interview_id')
            ->select('member.name as member_name', 'member.phone as member_phone', 'interview.*')
            ->selectRaw('count(interview_member.member_id) as member')
            ->orderByDesc($order)
            ->groupBy('interview.member_id')
            ->paginate($request->get('count'));
        return self::returnMsg($data);
    }

    public function interviewStatus(Request $request)
    {

    }

    public function interviewEdit(Request $request)
    {
        $rules     = [
            'status' => 'required|integer|in:0,1,2',
            'money'  => 'required|numeric',
            'id'     => 'required|integer|min:1'
        ];
        $sqlmap    = self::checkValidate($rules, $request->all());
        $interview = InterviewModel::find($sqlmap['id']);
        if (!isset($interview))
            return self::errorMsg('没有找到该集合室');
        try {
            $interview->status = $sqlmap['status'];
            $interview->money  = $sqlmap['money'];
            $interview->save();
            return self::successMsg();
        } catch (\Exception $exception) {
            return self::errorMsg($exception->getMessage());
        }

    }

    public function interviewMemberList(Request $request)
    {
        $id = (int)$request->input('id');

        $data = InterviewMemberModel::where('interview_id', $id)
            ->join('member', 'interview_member.member_id', '=', 'member.id')
            ->select('member.name as member_name', 'member.phone as member_phone', 'interview_member.*')
            ->get();
        return self::returnMsg($data);
    }

    /**
     * 题目类型列表
     */
    public function cateList(Request $request)
    {
        $data = InterviewCateModel::get();
        return self::returnMsg($data);
    }

    public function cateDel(Request $request)
    {
        $id = (int)$request->input('id');
        if ($id < 1)
            return self::errorMsg('请输入正确的id');
        if (InterviewCateModel::where('id', $id)->delete())
            return self::returnMsg();
        return self::errorMsg();
    }

    public function cateEdit(Request $request)
    {
        $rules  = [
            'name' => 'required|max:25',
        ];
        $sqlmap = self::checkValidate($rules, $request->all());
        $id     = $request->input('id');
        if ($id > 0) {
            $model = InterviewCateModel::find($id);
            if (!isset($model))
                self::returnError('没有找到该类型');
        } else {
            $model = new InterviewCateModel();
        }
        $model->name = $sqlmap['name'];
        try {
            $model->save();
            return self::successMsg();
        } catch (\Exception $exception) {
            return self::errorMsg($exception->getMessage());
        }
    }

}
